/*
 ============================================================================
 Name		: DownloadManageContainer.cpp
 Author	  : JohnLZeng
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CDownloadManageContainer implementation
 ============================================================================
 */
#include "DownloadManageContainer.h"

#include <coemain.h>
#include <apgtask.h>
#include <eikenv.h>
#include <apgcli.h>
#include <aknlists.h> 
#include <AknIconArray.h> 
#include <eikclbd.h>
#include <gulicon.h>

#include "SHPlatform.h"
#include "MacroUtil.h"
#include "utils.h"

#include "QWString.h"
#include "cx_moduleinterface.h"
#include "QQReaderMessageId.h"
#include "QQReaderModuleId.h"
#include "cx_string.h"
#include "EngineManager.h"
#include "LMSvgUtil.h"

CDownloadManageContainer::CDownloadManageContainer()
	{
	// No implementation required
	}

CDownloadManageContainer::~CDownloadManageContainer()
	{
	SHEngine()->SetDownloadProcessNotify(NULL);
	if (iListBox)
		delete iListBox;
	}

CDownloadManageContainer* CDownloadManageContainer::NewLC(const TRect& aRect)
	{
	CDownloadManageContainer* self = new (ELeave) CDownloadManageContainer();
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

CDownloadManageContainer* CDownloadManageContainer::NewL(const TRect& aRect)
	{
	CDownloadManageContainer* self = CDownloadManageContainer::NewLC(aRect);
	CleanupStack::Pop(); // self;
	return self;
	}

void CDownloadManageContainer::ConstructL(const TRect& aRect)
	{
	CreateWindowL();

	iListBox = new (ELeave) CAknDoubleGraphicStyleListBox();
	iListBox->ConstructL(this, 0);
	iListBox->SetContainerWindowL(*this);

	// Creates scrollbar.
	iListBox->CreateScrollBarFrameL(ETrue);
	iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
			CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
	iListBox->ItemDrawer()->ColumnData()->EnableMarqueeL(ETrue);
	
	SetIconsL();

	UpdateDisplay();

	//	CTextListBoxModel* model = iListBox->Model();
	//	CDesCArray* items = static_cast<CDesCArray*> (model->ItemTextArray());
	//
	//	items->Reset();
	//	SHEngine()->LoadAllToDownloadList(items);
	//	iListBox->HandleItemAdditionL();

	SHEngine()->SetDownloadProcessNotify(this);
	SetRect(aRect);
	ActivateL();
	}

// ---------------------------------------------------------
// CThemeChangeContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CDownloadManageContainer::CountComponentControls() const
	{
	TInt count(0);
	if (iListBox)
		count++;

	return count; // return nbr of controls inside this container
	}

// ---------------------------------------------------------
// CThemeChangeContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CDownloadManageContainer::ComponentControl(TInt aIndex) const
	{
	switch (aIndex)
		{
		case 0:
			return iListBox; // Returns the pointer to listbox object.
		default:
			return NULL;
		}
	}

// ---------------------------------------------------------
// CThemeChangeContainer::HandleControlEventL(
//	 CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CDownloadManageContainer::HandleControlEventL(CCoeControl* /*aControl*/,
		TCoeEvent /*aEventType*/)
	{
	// TODO: Add your control event handler code here
	}

//------------------------------------------------------------------
//CDownloadManageContainer::OfferKeyEventL(
// const TKeyEvent& aKeyEvent,	TEventCode aType)
//-------------------------------------------------------------------
//
TKeyResponse CDownloadManageContainer::OfferKeyEventL(
		const TKeyEvent& aKeyEvent, TEventCode aType)
	{
	// See if we have a selection
	TInt code = aKeyEvent.iCode;
	switch (code)
		{
		//add your code here...
		case EKeyDownArrow:
		case EKeyUpArrow:
			return iListBox->OfferKeyEventL(aKeyEvent, aType);
		case EKeyDevice3:
			if (aType == EEventKey)
				{
				Change();
				return EKeyWasConsumed;
				}
			break;
		case EKeyBackspace:
			if (aType == EEventKey)
				{
				Remove();
				return EKeyWasConsumed;
				}
			break;
		default:
			break;
		}
	return EKeyWasNotConsumed;
	}
// -----------------------------------------------------------------------------
// CDownloadManageContainer::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CDownloadManageContainer::Draw(const TRect& /*aRect*/) const
	{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();
	TRect drawRect(Rect());
	gc.Clear(drawRect);

	//add your code here...
	}

// -----------------------------------------------------------------------------
// CDownloadManageContainer::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CDownloadManageContainer::SizeChanged()
	{
	if (iListBox)
		{
		iListBox->SetRect(Rect());
		}
	DrawNow();
	}

void CDownloadManageContainer::SetIconsL()
	{
	CArrayPtr<CGulIcon>* iconList = new (ELeave) CAknIconArray(2);
	// push iconarray to stack
	CleanupStack::PushL(iconList);

	CFbsBitmap *bitmap1, *mask1;
	LMSvgUtil::GetImageAndMaskFormResourceL(bitmap1, mask1, EMbmListenvoaStop,
			EMbmListenvoaStop_mask);
	CGulIcon* newIcon1 = CGulIcon::NewL(bitmap1, mask1);
	iconList->AppendL(newIcon1);

	CFbsBitmap *bitmap2, *mask2;
	LMSvgUtil::GetImageAndMaskFormResourceL(bitmap2, mask2,
			EMbmListenvoaRunning, EMbmListenvoaRunning_mask);
	CGulIcon* newIcon2 = CGulIcon::NewL(bitmap2, mask2);
	iconList->AppendL(newIcon2);

	CFbsBitmap *bitmap3, *mask3;
	LMSvgUtil::GetImageAndMaskFormResourceL(bitmap3, mask3,
			EMbmListenvoaWaiting, EMbmListenvoaWaiting_mask);
	CGulIcon* newIcon3 = CGulIcon::NewL(bitmap3, mask3);
	iconList->AppendL(newIcon3);

	iListBox->ItemDrawer()->ColumnData()->SetIconArray(iconList);
	CleanupStack::Pop(iconList);
	}

void CDownloadManageContainer::UpdateDisplay()
	{
	CTextListBoxModel* model = iListBox->Model();
	CDesCArray* items = static_cast<CDesCArray*> (model->ItemTextArray());

	items->Reset();
	SHEngine()->UpdateToDownloadList(items);

	iListBox->HandleItemAdditionL();

	}

CDesCArray* CDownloadManageContainer::GetList()
	{
	CTextListBoxModel* model = iListBox->Model();
	CDesCArray* items = static_cast<CDesCArray*> (model->ItemTextArray());
	items->Reset();

	return items;
	}

void CDownloadManageContainer::UpdateDownloadProcessList()
	{
	iListBox->HandleItemAdditionL();
	}

void CDownloadManageContainer::Start()
	{
	CTextListBoxModel* model = iListBox->Model();
	CDesCArray* items = static_cast<CDesCArray*> (model->ItemTextArray());

	items->Reset();
	SHEngine()->StartTaskAll(items);
	iListBox->HandleItemAdditionL();
	}

void CDownloadManageContainer::Stop()
	{
	CTextListBoxModel* model = iListBox->Model();
	CDesCArray* items = static_cast<CDesCArray*> (model->ItemTextArray());

	items->Reset();
	SHEngine()->StopTaskAll(items);
	iListBox->HandleItemAdditionL();
	}
void CDownloadManageContainer::Change()
	{
	TInt index = iListBox->CurrentItemIndex();
	if (index >= 0)
		{
		SHEngine()->StartTask(index);
		}
	}

void CDownloadManageContainer::Remove()
	{
	TInt index = iListBox->CurrentItemIndex();
	if (index >= 0)
		{
		CTextListBoxModel* model = iListBox->Model();
		CDesCArray* items = static_cast<CDesCArray*> (model->ItemTextArray());

		items->Reset();
		SHEngine()->RemoveTask(index, items);
		iListBox->HandleItemAdditionL();
		}
	}
TBool CDownloadManageContainer::IsRun()
	{
	TInt index = iListBox->CurrentItemIndex();
	TBool re = EFalse;
	if (index >= 0)
		{
		re = SHEngine()->IsTaskRunning(index);
		}
	return re;
	}
